< previous page page_555 next page >

Page 555
d6ef40187df115e88326dbc94f09ab9a.gif d6ef40187df115e88326dbc94f09ab9a.gif
were entered. If you want to see the records in alphabetical order, place the following string in the RecordSource property of the Data control:
d6ef40187df115e88326dbc94f09ab9a.gif d6ef40187df115e88326dbc94f09ab9a.gif
SELECT * FROM authors ORDER BY author
d6ef40187df115e88326dbc94f09ab9a.gif d6ef40187df115e88326dbc94f09ab9a.gif
The string that you used to set the Data control's RecordSource property is an example of a SQL statement.
Using Code with the Data Control
In real life, your applications will be more dynamic than the sample you have just created. For that reason, it is important to understand that setting SQL statements and other nonpermanent items in code is generally better programming practice. Fortunately, the Data control allows you to manipulate most of the underlying objects from code.
To illustrate, continue with the sample project in Visual Basic. Place two command buttons named cmdSortAuthor and cmdSortYear on the form. Enter the following lines of code for their Click event procedures:
Private Sub cmdSortAuthor_Click()

 dtaMain.RecordSource = "SELECT * FROM authors ORDER BY author"
 dtaMain.Refresh

End Sub

Private Sub cmdSortYear_Click()

 dtaMain.RecordSource = "SELECT * FROM authors ORDER BY [Year Born] DESC"
 dtaMain.Refresh

End Sub
Run the program, and you should be able to change the sort order by clicking either of the buttons. Notice how setting the RecordSource property in code allows your program to be more flexible than when you simply set it at designtime.
note.gif You can control many additional aspects of the Data control with code. For example, you do not even need bound controls to display data. You can simply access the Recordset property of the Data control directly:
Msgbox "Current Author is " & dtaMain.Recordset.Fields("Author")
Chapter 26, "Using Data Access Objects (DAO)," discusses accessing databases entirely with code. square.gif

Adding and Deleting Records
As you can see, the Data control is quick and easy, but it lacks a few capabilities that are necessary for most data entry applicationsspecifically, adding and deleting records. The sample application allows you to edit existing records, but you cannot add a totally new record without a couple of modifications.

 
< previous page page_555 next page >